package com.security.demo.service;

import com.security.demo.entity.JwtUser;
import com.security.demo.entity.User;
import com.security.demo.repository.UserRepository;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.nio.channels.NonReadableChannelException;

import static java.util.Collections.emptyList;

/**
 * 账号密码的验证
 * @author  zengwenjie
 * @date    2020/8/19 23:5
 * @version 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private UserRepository userRepository;

    public UserDetailsServiceImpl(UserRepository userRepository){
        this.userRepository=userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user=userRepository.findByUsername(s);
        if (user==null){
            throw new UsernameNotFoundException(s);
        }
        return new JwtUser(user);
        //return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),emptyList());
    }
}
